package com.pankey;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends Activity {

	Bitmap bitmapSource;
	int mX = 0;
	int mY = 0;
	
	int screenHeight;
	int screenWidth;
	
	float imgRatio;
	int imgHeight;
	int imgWidth;
	
	//nebitno
	int step = 20;

	
	Point p0, p1;
	
	void fixXY() {
		mX = mX % imgWidth;
		mY = mY % imgHeight;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		if (savedInstanceState == null) {
			bitmapSource = BitmapFactory.decodeResource(getResources(),
					R.drawable.qwerty_us);
			setDeimensions();
		}
	}

	// nebitno
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	// nebitno
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	// refreshanje slike 
	public void onClickOk(View v) {
		//Toast.makeText(this, "Clicked on Button", Toast.LENGTH_LONG).show();

		try {
			/*ImageView imgViewFront = (ImageView) findViewById(R.id.imgFront);
			imgViewFront.setImageResource(R.drawable.us_dvorak_small);
			 */
			
			
			
			Bitmap bitmap = Bitmap.createBitmap(bitmapSource, mX + imgWidth, mY + imgHeight, imgWidth, imgHeight);
			//Bitmap.create
			
			System.out.println(bitmapSource.getWidth() + "," + bitmapSource.getHeight());
			System.out.println((float)bitmapSource.getWidth()/1440);

			
			// bitmapSource.recycle();
			// bitmapSource = null;

			
			// prikaz slike
			ImageView imgViewBack = (ImageView) findViewById(R.id.imgBack);
			imgViewBack.setImageBitmap(bitmap);

		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("GRESKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
			e.printStackTrace();
		}
	}

	// pomak
	public void onClickMove(View v) {
		//Toast.makeText(this, "Button clicked", Toast.LENGTH_LONG).show();
		try {
			Button btn = (Button) v;

			if (btn.getText().toString().equals("Left")) {
				this.mX -= step;
			}
			if (btn.getText().toString().equals("Right")) {
				this.mX += step;
			}
			if (btn.getText().toString().equals("Up")) {
				this.mY -= step;
			}
			if (btn.getText().toString().equals("Down")) {
				this.mY += step;
			}
			fixXY();
			System.out.println("Pomak: " + mX + "," + mY);
			onClickOk(v);
			setLayout();
		} catch (Exception e) {
			// Toast.makeText(this, "Greska", Toast.LENGTH_LONG).show();

			System.out.println("GRESKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
			e.printStackTrace();
		}
	}

	public boolean onTouchEvent(MotionEvent event) {
	    int eventaction = event.getAction();
	    //Toast.makeText(this, "DISPLAY clicked", Toast.LENGTH_LONG).show();
	    switch (eventaction) {
	        case MotionEvent.ACTION_DOWN: 
	            // finger touches the screen
	        	p0 = new Point((int)event.getX(), (int)event.getY()); 
				
	            break;

	        case MotionEvent.ACTION_MOVE:
	            // finger moves on the screen
	        	p1 = new Point((int)event.getX(), (int)event.getY()); 
	        	mX -= p1.x-p0.x;
	        	mY -= p1.y-p0.y;
	       
	        	//p0 = new Point(p1.x, p1.y); 
	        	p0.set(p1.x, p1.y);
	        	
	        	fixXY();
	        	onClickOk(new View(getApplicationContext()));
	            break;

	        case MotionEvent.ACTION_UP:
	        	p1 = new Point((int)event.getX(), (int)event.getY()); 
	        	mX -= p1.x-p0.x;
	        	mY -= p1.y-p0.y;
	       
	        	//p0 = new Point(p1.x, p1.y); 
	        	p0.set(p1.x, p1.y);
	        	
	        	fixXY();
	        	onClickOk(new View(getApplicationContext()));
	            // finger leaves the screen
	            break;
	    }

	    // tell the system that we handled the event and no further processing is required
	    return true; 
	}
	// see http://androidsnippets.com/handle-touch-events-ontouchevent
	
	void setDeimensions()
	{
		//0123456789ABCDEF
		DisplayMetrics metrics = new DisplayMetrics();
		  getWindowManager().getDefaultDisplay().getMetrics(metrics);
		  screenHeight = metrics.heightPixels;
		  screenWidth = metrics.widthPixels;
		  
		  imgRatio = (float)bitmapSource.getWidth()/1440;
		  
		  imgHeight = (int) ((bitmapSource.getHeight()/3));
		  imgWidth = (int) ((bitmapSource.getWidth()/3));
		  Toast.makeText(this, "screen/image = " +imgRatio + "\n" + imgWidth+ "x"+imgHeight, Toast.LENGTH_SHORT).show();
		  onClickMove(new View(getApplicationContext()));
		  
		  
	
	}
	
	void setLayout(){
		ImageView imgView = (ImageView) findViewById(R.id.imgBack);
			  
		RelativeLayout layout = (RelativeLayout)findViewById(R.id.layRelative);
		layout.getLayoutParams().height = imgView.getLayoutParams().height;
		layout.getLayoutParams().width = imgView.getLayoutParams().width;
	}

}
